<?php

declare(strict_types=1);

namespace App\CustHub\Model;

use Mine\MineModel;

/**
 * @property int $id 主键
 * @property \Carbon\Carbon $created_at 
 * @property \Carbon\Carbon $updated_at 
 * @property int $s_type 1点击  2填写  3提交
 * @property string $open_id 
 * @property string $source 1通用链接  2专属链接  3高级群发-通用链接  4高级群发-专属链接
 * @property int $questionnaire_id 问卷id
 */
class ChSmartQuestionnaireStatistic extends MineModel
{
    /**
     * The table associated with the model.
     */
    protected ?string $table = 'ch_smart_questionnaire_statistics';

    /**
     * The attributes that are mass assignable.
     */
    protected array $fillable = ['id', 'created_at', 'updated_at', 's_type', 'open_id', 'source', 'questionnaire_id'];

    /**
     * The attributes that should be cast to native types.
     */
    protected array $casts = ['id' => 'integer', 'created_at' => 'datetime', 'updated_at' => 'datetime', 's_type' => 'integer', 'questionnaire_id' => 'integer'];


    /**
     * 用户点击/提交/填写时 统计数据
     */
    public static function upStatistic($questionnaire_id,$source,$open_id,$s_type)
    {
        $record = self::query() -> where('questionnaire_id',$questionnaire_id) -> where('source',$source) ->  where('s_type',$s_type) -> where('open_id',$open_id) -> first();
        if(empty($record)){
            self::query() -> create([
                'questionnaire_id' => $questionnaire_id,
                'source' => $source,
                's_type' => $s_type,
                'open_id' => $open_id
            ]);
            //1点击  2填写  3提交
            if($s_type == 1){
                ChSmartQuestionnaire::where('id',$questionnaire_id) -> increment('click_total');
            }else if($s_type == 2){
                ChSmartQuestionnaire::where('id',$questionnaire_id) -> increment('write_total');
            }else if($s_type == 3){
                ChSmartQuestionnaire::where('id',$questionnaire_id) -> increment('submit_total');
            }
        }
    }
}
